#include "Storage/WrittenBuffer.h"
#include "utilities/RawPacket.h"
#include "utilities/RawPacket.h"
#include <Windows.h>

WrittenBuffer::WrittenBuffer(int no, int length)
{
	mCapacity = length;
	mContentLength = 0;
	mNo = no;

	mBufferBlob = (unsigned char*)VirtualAlloc(NULL,length,MEM_COMMIT,PAGE_READWRITE);
	assert(mBufferBlob != NULL);
}

bool WrittenBuffer::write(void* buffer,int length)
{
	if (mContentLength + length > mCapacity) {
		return false;
	}

	memcpy(mBufferBlob + mContentLength,buffer,length);
	mContentLength += length;

	return true;
}

void WrittenBuffer::reset()
{
	mContentLength = 0;
}

/*bool PacketBufferBlock::pushIn(RawPacket* rp)
{
	int added = RawPacket::PacketHeaderLength + rp->getCapLength();

	if (m_Length + added <= m_Capacity) {
		rp->serialize(m_Buffer + m_Length);

		if (0 == m_Length) {
			m_BeginNo = rp->getNo();
		}

		m_EndNo = rp->getNo();
		m_Length += added;

		return true;
	} else {
		return false;
	}

}

/*boost::shared_ptr<WrittenBuffer> WrittenBuffer::m_Instance;*/

WrittenBuffer::~WrittenBuffer(void)
{
	if (mBufferBlob) {
		VirtualFree(mBufferBlob,0,MEM_RELEASE);
	}
}
